package com.yayan.xh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.yayan.xh.bean.NewsBean;

public class NewsDao extends ConnectionBase implements INewsDao {
	
	private Connection conn = null;
	
	private QueryRunner qRunner = null;
	
	private PreparedStatement ps = null;
	
	private ResultSet rsKey = null;

	
	@Override
	public Integer getNewsPageCount(Map<String, String> map) throws Exception {
		int pageCount = 0;
		try {
			Map<String, Object> result = null;
			qRunner = new QueryRunner();
			String sql = "select count(*) / 10 as amount from tb_news where shop_id=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new MapHandler(), map.get("shopId"));
			pageCount = (int) Math.ceil(Double.parseDouble(result.get("amount").toString()));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return pageCount;
	}

	@Override
	public List<Map<String, Object>> getNewsPage(Map<String, String> map) throws Exception {
		List<Map<String, Object>> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select n_id as newsId, n_title as title, n_content as content, n_videoadd as videoadd, n_createdatetime as datetime "
					+ "from tb_news where shop_id=? order by n_createdatetime desc limit ?,10";
			conn = getConn();
			result =  qRunner.query(conn, sql, new MapListHandler(), map.get("shopId"), (Integer.parseInt(map.get("curPage").toString()) - 1) * 10);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> getNewsByNewsId(Map<String, String> map) throws Exception {
		List<Map<String, Object>> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select n_title as title, n_content as content, n_videoadd as videoadd, n_createdatetime as datetime"
					+ " from tb_news where shop_id=? and n_id=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new MapListHandler(), map.get("shopId"), map.get("newsId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public List<NewsBean> getNewsListBack(Map<String, String> map) throws Exception {
		List<NewsBean> result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_news where shop_id=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new BeanListHandler<NewsBean>(NewsBean.class), map.get("shopId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public boolean saveNews(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "insert into tb_news(shop_id, n_title, n_content, n_videoadd, n_createdatetime) values(?,?,?,?,now())";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("title"));
			ps.setObject(3, map.get("content"));
			ps.setObject(4, map.get("videoadd"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public NewsBean getNewsByIdBack(Map<String, String> map) throws Exception {
		NewsBean result = null;
		try {
			qRunner = new QueryRunner();
			String sql = "select * from tb_news where shop_id=? and n_id=?";
			conn = getConn();
			result =  qRunner.query(conn, sql, new BeanHandler<NewsBean>(NewsBean.class), map.get("shopId"), map.get("newsId"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return result;
	}

	@Override
	public boolean modifyNews(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "update tb_news set n_title=?, n_content=?, n_videoadd=? where shop_id=? and n_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("title"));
			ps.setObject(2, map.get("content"));
			ps.setObject(3, map.get("videoadd"));
			ps.setObject(4, map.get("shopId"));
			ps.setObject(5, map.get("newsId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

	@Override
	public boolean delNews(Map<String, String> map) throws Exception {
		boolean f = false;
		try {
			String sql = "delete from tb_news where shop_id=? and n_id=?";
			conn = getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setObject(1, map.get("shopId"));
			ps.setObject(2, map.get("newsId"));
			f = ps.executeUpdate() > 0 ? true : false;
			conn.commit();
			conn.setAutoCommit(true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn, ps, rsKey);
		}
		return f;
	}

}
